<template>
  <div>
    <!-- 头部区域 -->
    <div class="header-container">
      <div class="brand-wrapper">
        <span class="system-brand">健身房管理系统</span>
      </div>
      <div class="user-info">
        <div class="user-avatar">
          <el-avatar
              :size="38"
              src="https://img0.baidu.com/it/u=3785348319,2786421711&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=657"
          />
        </div>
        <div class="user-name">
          <span>张伟</span>
        </div>
      </div>
    </div>

    <!-- 内容区域 -->
    <div style="display: flex;">
      <!-- 左侧导航 -->
      <div style="width: 200px; border-right: 1px solid #ddd; min-height: calc(100vh - 60px);">
        <el-menu router :default-active="router.currentRoute.value.path" style="border: 0;">
          <el-menu-item index="/admin/a_index">
            <el-icon><House /></el-icon>
            首页
          </el-menu-item>
          <el-menu-item index="/admin/a_user">
            <el-icon><User /></el-icon>
            会员管理
          </el-menu-item>
          <el-menu-item index="/admin/a_pay">
            <el-icon><Postcard /></el-icon>
            订单管理
          </el-menu-item>
          <el-sub-menu index="1">
            <template #title>
              <el-icon><User /></el-icon>
              <span>员工管理</span>
            </template>
            <el-menu-item index="/admin/a_employee">员工列表</el-menu-item>
            <el-menu-item index="/admin/a_c_introduction">教练信息</el-menu-item>
          </el-sub-menu>
          <el-sub-menu index="2">
            <template #title>
              <el-icon><postcard/></el-icon>
              <span>课程管理</span>
            </template>
            <el-menu-item index="/admin/a_courses">课程列表</el-menu-item>
            <el-menu-item index="/admin/a_cs_introduction">课程信息</el-menu-item>
            <el-menu-item index="/admin/a_enrollment">名单信息</el-menu-item>
          </el-sub-menu>
          <el-sub-menu index="3">
            <template #title>
              <el-icon><bicycle /></el-icon>
              <span>器材管理</span>
            </template>
            <el-menu-item index="/admin/a_equipment">器材列表</el-menu-item>
            <el-menu-item index="/admin/a_repair">维修记录</el-menu-item>
          </el-sub-menu>
          <el-menu-item @click="handleLogout">
            <el-icon><SwitchButton /></el-icon>
            退出登录
          </el-menu-item>
        </el-menu>
      </div>
      <!-- 右侧内容 -->
      <div style="flex: 1; width: 0; background-color: aliceblue; padding: 10px;">
        <!-- 面包屑导航 -->
        <el-breadcrumb separator="/" style="margin-bottom: 20px;">
          <el-breadcrumb-item
              v-for="(item, index) in breadcrumbList"
              :key="item.path"
          >
            <router-link
                v-if="index !== breadcrumbList.length - 1"
                :to="item.path"
            >
              {{ item.meta.title }}
            </router-link>
            <span v-else>{{ item.meta.title }}</span>
          </el-breadcrumb-item>
        </el-breadcrumb>

        <router-view />
      </div>
    </div>
  </div>
</template>

<script setup>
import {Bicycle, DataAnalysis, House, Postcard, SwitchButton, User, UserFilled} from '@element-plus/icons-vue';
import router from '@/router';
import {ElMessage} from "element-plus";

const checkAuth = ()=>{
  const role = localStorage.getItem('role');
  if(role===null){
    ElMessage.error('未登录，请重新登录');
    router.push('/login');
    return;
  }
  if(role!=='管理员'){
    ElMessage.error('权限不足，请重新登录');
    router.push('/login');
  }
}

// 处理退出登录
const handleLogout = () => {
  // 这里可以添加退出登录的逻辑，例如清除 token 等
  localStorage.removeItem('role');
  localStorage.removeItem('eid');
  router.push({ name: 'login' }); // 跳转到登录页
};

checkAuth();
import { computed } from 'vue'
import { useRoute } from 'vue-router'

const route = useRoute()

// 生成面包屑数据
const breadcrumbList = computed(() => {
  const matched = route.matched
      .filter(record => record.meta && record.meta.title) // 过滤有标题的路由
      .map(record => ({
        path: resolvePath(record.path, route.params), // 处理动态路径参数
        meta: record.meta
      }))

  // 如果首页不在匹配中则手动添加
  if (!matched.some(item => item.path === '/admin/a_index')) {
    matched.unshift({
      path: '/admin/a_index',
      meta: { title: '管理员首页' }
    })
  }

  return matched
})

// 处理动态路径参数
const resolvePath = (path, params) => {
  return Object.keys(params).reduce(
      (acc, param) => acc.replace(`:${param}`, params[param]),
      path
  )
}

</script>

<style scoped>
/* 可以根据需要添加样式 */
/* 头部容器 */
.header-container {
  height: 64px;
  background: linear-gradient(135deg, #1a1a1a 0%, #0a0a0a 100%);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  position: relative;
  z-index: 1000;
}

/* 品牌标识 */
.brand-wrapper {
  display: flex;
  align-items: center;
}

.system-brand {
  color: #fff;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 1px;
  background: linear-gradient(45deg, #00ff87, #60efff);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* 用户信息 */
.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  transition: all 0.3s;
  padding: 8px 16px;
  border-radius: 8px;
}

.user-info:hover {
  background: rgba(255, 255, 255, 0.1);
}

.user-avatar {
  display: flex;
  align-items: center;
}

.user-avatar :deep(.el-avatar) {
  border: 2px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

.user-name {
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 6px;
}

.dropdown-icon {
  color: rgba(255, 255, 255, 0.8);
  font-size: 12px;
  transition: transform 0.3s;
}

.user-info:hover .dropdown-icon {
  transform: rotate(180deg);
}
</style>
